<template>
  <div class="reset-password-container">
    <div class="reset-password-card">
      <div class="reset-password-header">
        <h1 class="reset-password-title">🔐 重置密码</h1>
        <p class="reset-password-subtitle">请输入您的新密码</p>
      </div>

      <el-form :model="resetPasswordForm" :rules="rules" ref="resetPasswordFormRef" class="reset-password-form">
        <el-form-item prop="newPassword">
          <el-input
            v-model="resetPasswordForm.newPassword"
            type="password"
            placeholder="请输入新密码（至少6位）"
            size="large"
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="resetPasswordForm.confirmPassword"
            type="password"
            placeholder="请确认新密码"
            size="large"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleResetPassword"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="reset-password-button"
            :loading="loading"
            @click="handleResetPassword"
          >
            重置密码
          </el-button>
        </el-form-item>
      </el-form>

      <div class="reset-password-footer">
        <router-link to="/login" class="link">返回登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { resetPassword } from '@/api'

const router = useRouter()
const route = useRoute()

const resetPasswordFormRef = ref(null)
const loading = ref(false)
const token = ref('')

const resetPasswordForm = reactive({
  newPassword: '',
  confirmPassword: ''
})

// 验证确认密码
const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== resetPasswordForm.newPassword) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const handleResetPassword = async () => {
  if (!resetPasswordFormRef.value) return

  await resetPasswordFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        await resetPassword({
          token: token.value,
          newPassword: resetPasswordForm.newPassword
        })
        
        ElMessage.success('密码重置成功，请使用新密码登录')
        
        // 2秒后跳转到登录页
        setTimeout(() => {
          router.push('/login')
        }, 2000)
      } catch (error) {
        console.error('重置密码失败：', error)
      } finally {
        loading.value = false
      }
    }
  })
}

onMounted(() => {
  // 从 URL 参数中获取 token
  token.value = route.query.token || ''
  
  if (!token.value) {
    ElMessage.error('无效的重置密码链接')
    router.push('/login')
  }
})
</script>

<style scoped>
.reset-password-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #ffeef8 0%, #fff5f7 100%);
  padding: 20px;
}

.reset-password-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border-radius: 24px;
  padding: 40px;
  box-shadow: 0 8px 24px rgba(255, 154, 158, 0.3);
}

.reset-password-header {
  text-align: center;
  margin-bottom: 40px;
}

.reset-password-title {
  font-size: 36px;
  font-weight: bold;
  background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 10px;
}

.reset-password-subtitle {
  font-size: 14px;
  color: #999;
}

.reset-password-form {
  margin-top: 30px;
}

.reset-password-button {
  width: 100%;
  background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
  border: none;
  font-size: 16px;
  font-weight: bold;
}

.reset-password-footer {
  text-align: center;
  margin-top: 20px;
}

.link {
  color: #ff9a9e;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
}

.link:hover {
  color: #ff8a8e;
  text-decoration: underline;
}

@media (max-width: 768px) {
  .reset-password-card {
    padding: 30px 20px;
  }

  .reset-password-title {
    font-size: 28px;
  }
}
</style>

